using System;
using System.Configuration;
using System.Data;
using System.Collections;
using digit.Core;


namespace SMS.SmManager
{
    public class MsgOut_86Manager
    {
        #region CONSTRUCTORS
        static MsgOut_86Manager()
        {
            _data = new MsgOut_86Data(Util.GetConnectionString("SMSC"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static MsgOut_86Data _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(long msgInID, string mobileNo, string receiverMobileNo, string serviceID, string commandCode, string subCode1, string subCode2, string subCode3, string msgOutContent, string msgInContent, int requestID, byte msgType, byte contentType, DateTime requestTime, DateTime responseTime, string messageID, byte isCDR, byte isMore, byte msgTotal, byte msgIndex, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[21];
                values[0] = msgInID;
                values[1] = Util.ReplaceNullTo(mobileNo, DBNull.Value);
                values[2] = Util.ReplaceNullTo(receiverMobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(serviceID, DBNull.Value);
                values[4] = Util.ReplaceNullTo(commandCode, DBNull.Value);
                values[5] = Util.ReplaceNullTo(subCode1, DBNull.Value);
                values[6] = Util.ReplaceNullTo(subCode2, DBNull.Value);
                values[7] = Util.ReplaceNullTo(subCode3, DBNull.Value);
                values[8] = Util.ReplaceNullTo(msgOutContent, DBNull.Value);
                values[9] = Util.ReplaceNullTo(msgInContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(requestID, DBNull.Value);
                values[11] = Util.ReplaceNullTo(msgType, DBNull.Value);
                values[12] = Util.ReplaceNullTo(contentType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(requestTime, DBNull.Value);
                values[14] = Util.ReplaceNullTo(responseTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(messageID, DBNull.Value);
                values[16] = Util.ReplaceNullTo(isCDR, DBNull.Value);
                values[17] = Util.ReplaceNullTo(isMore, DBNull.Value);
                values[18] = Util.ReplaceNullTo(msgTotal, DBNull.Value);
                values[19] = Util.ReplaceNullTo(msgIndex, DBNull.Value);
                values[20] = status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(MsgOut_86Info info)
        {
            _error = "";
            try
            {
                object[] values = new object[21];
                values[0] = info.MsgInID;
                values[1] = Util.ReplaceNullTo(info.MobileNo, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.ReceiverMobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.ServiceID, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.CommandCode, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.SubCode1, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.SubCode2, DBNull.Value);
                values[7] = Util.ReplaceNullTo(info.SubCode3, DBNull.Value);
                values[8] = Util.ReplaceNullTo(info.MsgOutContent, DBNull.Value);
                values[9] = Util.ReplaceNullTo(info.MsgInContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(info.RequestID, DBNull.Value);
                values[11] = Util.ReplaceNullTo(info.MsgType, DBNull.Value);
                values[12] = Util.ReplaceNullTo(info.ContentType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(info.RequestTime, DBNull.Value);
                values[14] = Util.ReplaceNullTo(info.ResponseTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(info.MessageID, DBNull.Value);
                values[16] = Util.ReplaceNullTo(info.IsCDR, DBNull.Value);
                values[17] = Util.ReplaceNullTo(info.IsMore, DBNull.Value);
                values[18] = Util.ReplaceNullTo(info.MsgTotal, DBNull.Value);
                values[19] = Util.ReplaceNullTo(info.MsgIndex, DBNull.Value);
                values[20] = info.Status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long msgOutID, long msgInID, string mobileNo, string receiverMobileNo, string serviceID, string commandCode, string subCode1, string subCode2, string subCode3, string msgOutContent, string msgInContent, int requestID, byte msgType, byte contentType, DateTime requestTime, DateTime responseTime, string messageID, byte isCDR, byte isMore, byte msgTotal, byte msgIndex, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[22];
                values[0] = msgOutID;
                values[1] = msgInID;
                values[2] = Util.ReplaceNullTo(mobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(receiverMobileNo, DBNull.Value);
                values[4] = Util.ReplaceNullTo(serviceID, DBNull.Value);
                values[5] = Util.ReplaceNullTo(commandCode, DBNull.Value);
                values[6] = Util.ReplaceNullTo(subCode1, DBNull.Value);
                values[7] = Util.ReplaceNullTo(subCode2, DBNull.Value);
                values[8] = Util.ReplaceNullTo(subCode3, DBNull.Value);
                values[9] = Util.ReplaceNullTo(msgOutContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(msgInContent, DBNull.Value);
                values[11] = Util.ReplaceNullTo(requestID, DBNull.Value);
                values[12] = Util.ReplaceNullTo(msgType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(contentType, DBNull.Value);
                values[14] = Util.ReplaceNullTo(requestTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(responseTime, DBNull.Value);
                values[16] = Util.ReplaceNullTo(messageID, DBNull.Value);
                values[17] = Util.ReplaceNullTo(isCDR, DBNull.Value);
                values[18] = Util.ReplaceNullTo(isMore, DBNull.Value);
                values[19] = Util.ReplaceNullTo(msgTotal, DBNull.Value);
                values[20] = Util.ReplaceNullTo(msgIndex, DBNull.Value);
                values[21] = status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(MsgOut_86Info info)
        {
            _error = "";
            try
            {
                object[] values = new object[22];
                values[0] = info.MsgOutID;
                values[1] = info.MsgInID;
                values[2] = Util.ReplaceNullTo(info.MobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.ReceiverMobileNo, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.ServiceID, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.CommandCode, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.SubCode1, DBNull.Value);
                values[7] = Util.ReplaceNullTo(info.SubCode2, DBNull.Value);
                values[8] = Util.ReplaceNullTo(info.SubCode3, DBNull.Value);
                values[9] = Util.ReplaceNullTo(info.MsgOutContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(info.MsgInContent, DBNull.Value);
                values[11] = Util.ReplaceNullTo(info.RequestID, DBNull.Value);
                values[12] = Util.ReplaceNullTo(info.MsgType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(info.ContentType, DBNull.Value);
                values[14] = Util.ReplaceNullTo(info.RequestTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(info.ResponseTime, DBNull.Value);
                values[16] = Util.ReplaceNullTo(info.MessageID, DBNull.Value);
                values[17] = Util.ReplaceNullTo(info.IsCDR, DBNull.Value);
                values[18] = Util.ReplaceNullTo(info.IsMore, DBNull.Value);
                values[19] = Util.ReplaceNullTo(info.MsgTotal, DBNull.Value);
                values[20] = Util.ReplaceNullTo(info.MsgIndex, DBNull.Value);
                values[21] = info.Status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long msgOutID)
        {
            _error = "";
            try
            {
                object[] values = { msgOutID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(MsgOut_86Info info)
        {
            _error = "";
            try
            {
                object[] values = { info.MsgOutID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static MsgOut_86Info Select(long msgOutID)
        {
            _error = "";
            try
            {
                object[] values = { msgOutID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                MsgOut_86Info info = new MsgOut_86Info(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static ArrayList GetAllMessagesForSend()
        {
            DataSet set = _data.GetAllMessagesForSend();
            if (set.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            ArrayList outs = new ArrayList();
            foreach (DataRow row in set.Tables[0].Rows)
            {
                MsgOut_86Info @out = new MsgOut_86Info();
                @out.MsgOutID = long.Parse(row["MsgOutID"].ToString());
                @out.MsgInID = long.Parse(row["MsgInID"].ToString());
                @out.MobileNo = row["MobileNo"].ToString();
                @out.ReceiverMobileNo = row["ReceiverMobileNo"].ToString();
                @out.ServiceID = row["ServiceID"].ToString();
                @out.CommandCode = row["CommandCode"].ToString();
                @out.SubCode1 = row["SubCode1"].ToString();
                @out.SubCode2 = row["SubCode2"].ToString();
                @out.MsgOutContent = row["MsgOutContent"].ToString();
                @out.MsgInContent = row["MsgInContent"].ToString();
                @out.RequestID = int.Parse(row["RequestID"].ToString());
                @out.MsgType = byte.Parse(row["MsgType"].ToString());
                @out.ContentType = byte.Parse(row["ContentType"].ToString());
                @out.RequestTime = Convert.ToDateTime(row["RequestTime"].ToString());
                @out.ResponseTime = Convert.ToDateTime(row["ResponseTime"].ToString());
                @out.IsMore = byte.Parse(row["IsMore"].ToString());
                @out.MsgTotal = byte.Parse(row["MsgTotal"].ToString());
                @out.Status = byte.Parse(row["Status"].ToString());
                @out.MsgIndex = byte.Parse(row["MsgIndex"].ToString());
                outs.Add(@out);
            }
            return outs;
        }

        public static bool UpdateStatus(long MsgOutID, long RequestID, DateTime ResponseTime, byte Index, byte Status)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = MsgOutID;
                values[1] = RequestID;
                values[2] = ResponseTime;
                values[3] = Index;
                values[4] = Status;

                return _data.UpdateStatus(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static int InsertMsgOut(string MobileNo, string Message, string ServiceID, string CommandCode, string MessageType, string RequestID, string TotalMsg, string MsgIndex, string IsMore, string ContentType)
        {
            _error = "";
            try
            {
                object[] values = new object[10];
                values[0] = MobileNo;
                values[1] = Message;
                values[2] = ServiceID;
                values[3] = CommandCode;
                values[4] = MessageType;
                values[5] = RequestID;
                values[6] = TotalMsg;
                values[7] = MsgIndex;
                values[8] = IsMore;
                values[9] = ContentType;

                return _data.InsertMT(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return 0;
            }
        }

        public static bool InsertError(string RequestID, DateTime ResponseTime, string Error, string Type)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = RequestID;
                values[1] = ResponseTime;
                values[2] = Error;
                values[3] = Type;

                return _data.InsertError(Error);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        #endregion
    }
}